package com.jr9z.jrjz.controller;

import com.jr9z.jrjz.common.BaseResponse;
import com.jr9z.jrjz.common.ResponseBuilder;
import com.jr9z.jrjz.entity.Ministry;
import com.jr9z.jrjz.service.MinistryService;
import com.jr9z.jrjz.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: MrZcc
 * @Description:
 * @Date: Created in 10:27 2019-4-1
 * @Modified By:
 */

@RestController
@RequestMapping("/ministry")
public class MinistryController {

    @Autowired
    private MinistryService ministryService;


    /**
     * @Description:添加服务类型
     * @date: 10:32 2019-4-1
     * @param: ministry json格式的服务实体
     * @return: com.jr9z.jrjz.common.BaseResponse
     */
    @RequestMapping(value = "/addMinistry", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse addMinistry(@RequestBody Ministry ministry) {
        int result = 0;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            result = ministryService.insertSelective(ministry);

            custom = Constant.RESULT(result);

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();
    }


    /**
     * @Description:根据id查询服务类型信息
     * @date: 13:58 2019-4-1
     * @param: serviceId 服务类型id
     * @return: 查询结果以json格式返回
     */
    @RequestMapping(value = "/getService", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getServiceById(Integer serviceId) {

        Ministry ministry;

        ResponseBuilder custom = ResponseBuilder.custom();

        try {
            ministry = ministryService.selectByPrimaryKey(serviceId);

            if (ministry != null) {
                custom.data(ministry).success(Constant.SUCCESS, Constant.SUCCESS_CODE);
            } else {
                custom.failed(Constant.FAIL, Constant.ERROR_CODE);
            }

        } catch (Exception e) {
            custom.failed(Constant.FAIL, Constant.EXCEPTION_CODE);
        }
        return custom.build();

    }




}
